package cn.wolfcode.p2p.base.service.impl;
import cn.wolfcode.p2p.base.domain.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.VideoAuthQuery;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {
        @Autowired
        private VideoAuthMapper videoAuthMapper;
        @Autowired
        private IUserInfoService userInfoService;

        /**
         * 保存视频审核申请
         * @param videoAuth
         */
        @Override
        public void saveVideoAuditOrder(VideoAuth videoAuth) {
                //判断基本参数
                AssertUtil.instance().isNotNull(videoAuth,"出错啦!请联系客服!");
                //判断当前用户没有通过视频认证
                UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());//当前用户

                //判断用户没有提交视频认证中
                AssertUtil.instance().isFalse(userInfo.hasVedioAuth(),"您已经通过视频认证,请勿重复提交!")
                .isNull(userInfo.getVideoAuthId(),"您已经提交了认证申请!");//保存视频认证
                //改变用户的userinfo里的视频认证ID
                VideoAuth va = new VideoAuth();
                va.setOrderTime(videoAuth.getOrderTime());
                va.setOrderDate(videoAuth.getOrderDate());
                va.setApplyTime(new Date());
                va.setAuditor(videoAuth.getAuditor());
                va.setApplier(UserContext.getLoginInfo());
                videoAuthMapper.insert(va);
                userInfo.setVideoAuthId(va.getId());
                userInfoService.update(userInfo);//更新userInfo
        }

        @Override
        public VideoAuth getById(Long id) {
                return videoAuthMapper.selectByPrimaryKey(id);
        }

        @Override
        public void cancelVideoAuth(Long id) {
                videoAuthMapper.delete(id);

        }

        @Override
        public PageResult query(VideoAuthQuery qo) {
                 //先查出COunt,如果COUNT为0,返回空集合
                 int row = videoAuthMapper.selectCount(qo);
                 if(row==0){
                     return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
                 }
                 List<RealAuth> list =videoAuthMapper.query(qo);
                 return new PageResult(list,row,qo.getCurrentPage(),qo.getPageSize());
        }

        @Override
        public void videoAuthAudit(Long id, String remark, Integer state) {
                //判断基本参数
                AssertUtil.instance().isNotNull(id,"出错啦!")
                        .isNotNull(state,"出错啦!");
                //判断是不是待审核
                VideoAuth va = videoAuthMapper.selectByPrimaryKey(id);
                AssertUtil.instance().isFalse(va.getState()!=VideoAuth.STATE_NORMAL,"状态异常");
                va.setAuditTime(new Date());//审核时间
                va.setRemark(remark);
                va.setState(state);
                update(va);
                UserInfo userInfo = userInfoService.getById(va.getApplier().getId());

                if(state==VideoAuth.STATE_SUCCESS){
                        //修改用户的位状态
                        userInfo.addBitState(BitStatesUtils.OP_VEDIO_AUTH);
                }else{
                        userInfo.setVideoAuthId(null);
                }
                userInfoService.update(userInfo);
        }

        private void update(VideoAuth va) {
                videoAuthMapper.updateByPrimaryKey(va);
        }
}
